package  networld
{
	import flash.events.EventDispatcher;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.text.TextField;
	import tcpip.NetworkEvent;
	import tcpip.PacketedConnection;
	/**
	 * ...
	 * @author egslava (egslava@gmail.com)
	 * @eventType NetWorldEvent.WORLD_HAD_BEED_CREATED
	 */
	public class NetWorld extends EventDispatcher
	{
		static private const NEW_CLIENT: String = "new_client";
		
		internal var coordinationServerAddress: String;
		internal var coordinationServerPort: Number;
		internal var coordinationServer: PacketedConnection;
		
		internal var rtmfpDeveloperKey: String;
		internal var rtmfpServerAddress: String;
		internal var rtmfpServerPort: Number;
		
		internal var localPlayer: NetPlayer;
		internal var _isMaster: Boolean = false;
		
		internal var log: TextField;
		
		private function isMaster(): Boolean {
			return _isMaster;
		}
		
		/* 2 Temporary vars, needs for code obviously*/
		private var isConnectedToCoordinationServer: Boolean = false;
		private var isConnectedToRtmfpServer: Boolean = false;
		
		internal var rtmfpServer: NetConnection;
		
		public function NetWorld(
					log: TextField, 
					coordinationServerAddress: String,
					coordinationServerPort: Number,
					rtmfpDeveloperKey: String, 
					rtmfpServerAddress: String  = "rtmfp://p2p.rtmfp.net", 
					rtmfpServerPort: Number = 1935) {
			this.coordinationServerAddress = coordinationServerAddress;
			this.coordinationServerPort = coordinationServerPort;
			this.rtmfpDeveloperKey = rtmfpDeveloperKey;
			this.rtmfpServerAddress = rtmfpServerAddress;
			this.rtmfpServerPort = rtmfpServerPort;
			this.log = log;
				/*Creating input and */
				
			connectToWorld();			
		}
		
		public function say(message: String): void {
			localPlayer.broadcastMessage(message);
		}
		private function connectToWorld(): void {
			/* Connection with RTMFP-server */
			rtmfpServer = new NetConnection();
			rtmfpServer.addEventListener(NetStatusEvent.NET_STATUS,
				connectToCoordinationServer);
				
			rtmfpServer.connect(rtmfpServerAddress + ":" +
								rtmfpServerPort + "/" + rtmfpDeveloperKey);
			
		}
		
		private function connectToCoordinationServer(e: NetStatusEvent): void {
			localPlayer = new NetPlayer(this);
			
			coordinationServer = new PacketedConnection(coordinationServerAddress,
				coordinationServerPort, log );
			coordinationServer.addEventListener(NetworkEvent.PACKET_RECEIVED,
				onPacket);
				
			coordinationServer.sendPacket("new_client", 
				rtmfpServer.nearID, 
				0001000);
				
			dispatchEvent(new NetWorldEvent(NetWorldEvent.WORLD_HAD_BEEN_FORMED) );
		}
		
		private function onPacket(e: NetworkEvent): void {
			if (e.caption == NEW_CLIENT) {
				players.push(new NetPlayer(this, e.data[0]));
			}
			log.appendText(e.caption + ":\n");
			for (var parameter: String in e.data) {
				log.appendText(parameter + " = " + e.data[parameter] + "\n")
			}
			log.appendText("\n");
		}
		
		/**
		 * This method returns array of all players, which is connected to 
		 * coordinationServerAddress:coordinationServerPort
		 * 
		 * @return Array of NetPlayer
		 * @see networld
		 * @see networld.MutualObject
		 */
		
		internal var players: Array = [];
		/// 
		/*public function get players(): Array;	//Array of strings with players names
		public function get objects(): Array;	//Of MutualObject
		internal function generateUniqueName(): String {
			
		}
		*/
		
			
	}

}